<?php
/**
 * 答题控制器类
 */
namespace Home\Controller;
use Think\Controller;

class AnswerController extends CommonController
{
    // 答题图片上传方法
    public function aimgupload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     52428800 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/images/qimg/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();

        if(!$info) {// 上传错误提示错误信息
            $retrunData = array(
                'code'=>1,
                'msg'=>$upload->getError()
            );
            echo json_encode($retrunData);
        }else{// 上传成功
            $info = current($info);
            $retrunData = array(
                'code'=>0,
                'msg'=>'上传成功',
                'data'=>array(
                    'src'=>__ROOT__.'/Public/images/qimg/'.$info['savepath'].$info['savename'],
                    'title'=>'图片'
                    )
                );
            echo json_encode($retrunData);
        }
    }
    // 答题上传方法
    public function apost()
    {
        if (!$_SESSION['uid']) {
            $data = array('error'=>'回答前请先登录','skip'=>1);
            echo json_encode($data);
        } else {
            $get_data = I('post.');//获取post数据
            if ($get_data['content'] == '') {
                $data = array('error'=>'内容不能为空');
                echo json_encode($data);
            }
            unset($get_data['file']);//去除永不到的file键值
            unset($get_data['jid']);//去除无用验证码
            $get_data['uid'] = $_SESSION['uid'];
            $get_data['aext'] = array('praise' => '0');
            $get_data['atime'] = time();
            $condition = ['qid'=>$get_data['qid']];
            M('question_ext')->where($condition)->save(['num_of_ans'=>$get_data['num_of_ans']]);
            unset($get_data['num_of_ans']);
            $res = D("answer")->relation(true)->add($get_data);
            if ($res) {
                unset($_SESSION['answer']);//清除验证答案
                $data = array('info'=>'您的回答发布完成。',
                              'aid'=>$res,
                              'atime'=>timeFormat($get_data['atime']));
                echo json_encode($data);
            } else {
                $data = array('error'=>'因未知原因，答题失败。');
                echo json_encode($data);
            }
        }
    }
    // 点赞
    public function praise()
    {
        if ($_POST) {
            $getpraise = I('post.');
            if ($getpraise['do'] == 'addPraise') {
                $condition = array('aid' => $getpraise['aid']);
                $data = array('praise' => $getpraise['praise']);
                $res = M('answer_ext')->where($condition)->save($data);
                if (!$res) {
                    echo json_encode(['error'=>'operation failed','info'=>$getpraise]);
                }
            } else if ($getpraise['do'] == 'delPraise') {
                $condition = array('aid' => $getpraise['aid']);
                $data = array('praise' => $getpraise['praise']);
                $res = M('answer_ext')->where($condition)->save($data);
                if (!$res) {
                    echo json_encode(['error'=>'operation failed','info'=>$getpraise]);
                }
            }
        }
    }
    // 采纳最佳答案
    public function greatAns()
    {
        if ($_POST) {
            $greatAns = I('post.');
            $condition = 'ans.qid = qext.qid and ans.aid = '.$greatAns['aid'];
            $data = ['is_great'=>1,
                     'get_great_ans'=>$greatAns['aid'],
                     'close'=>1];
            $res = M()->table(['answer'=>'ans','question_ext'=>'qext'])->where($condition)->save($data);
            if (!$res) {
                echo json_encode(['error'=>'operation failed','info'=>$greatAns]);
            } else {
                echo json_encode($res);
            }
        }
    }
}
